#!/usr/bin/perl
#lvformat.cgi
require './lvm-lib.pl';
&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);
my ($lvdev, $voldir, @lvinfo, $l, $lv);
$config{'mkfs_lock_new'} = "/var/nas/lock/mkfsd_new";
$err_msg="";
$log_ev="";

$lvdev = $in{'lv'};
@lvinfo = get_lv_info();

if(!$lvdev){
	$err_msg = $text{'err_noselectlv'};
	goto ERROR_OUT;
}

my $found = 0;
foreach $l (@lvinfo) {
	if ($l->{'lv'} eq $lvdev) {
		$lv = $l;
		$found = 1;
		last;
	}
} 
if(!$found){
	$err_msg = $text{'err_lvnofound'};
	goto ERROR_OUT;
}

if($lv->{'lvname'} =~ /volume\d+/){
	$voldir = "/usrvol/".$lv->{'lvname'};
}
else{
	$err_msg = $text{'err_errlvpath'};
	goto ERROR_OUT;
}

#begin mkfs. Because mkfs is very time consumed, fork a child process

my $child_pid=fork;
if (!defined($child_pid)) {
        $log_ev = $text{'log_ev_mkfs_fork_err'};
        $log_msg= &text('log_msg_mkfs_fork_err', $volname);
        nas_syslog($text{'my_log_minor'}, 'naslog_warning', $log_ev, $log_msg);

        $err_msg = $text{'err_systemerror'};
        goto ERROR_OUT;
}

if ($child_pid) {

        open(MKFS_LOCK, ">".$config{'mkfs_lock_new'});
        print MKFS_LOCK $child_pid, "\n";
        close(MKFS_LOCK);
        &redirect("volstat.cgi");
}
else {
        #child process
        while(1) {
                if (-s $config{'mkfs_lock_new'}) {
                        last;
                }
                sleep(1);
        }
        exec './mkfsd.pl', $lvdev, $voldir;
}

ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);


